package com.object.oriented.exam.system1.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.oriented.exam.system1.dao.ChoiceMapping;
import com.object.oriented.exam.system1.dao.PictureMapping;
import com.object.oriented.exam.system1.dao.ProblemMapping;
import com.object.oriented.exam.system1.entity.Choice;
import com.object.oriented.exam.system1.entity.Picture;
import com.object.oriented.exam.system1.entity.Problem;
import com.object.oriented.exam.system1.entity.Search;
import com.object.oriented.exam.system1.service.ProblemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author : Apple
 * @description :
 * @date : 2023-06-14 09:05
 **/

@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapping, Problem> implements ProblemService {

    @Resource
    private ChoiceMapping choiceMapping;

    @Resource
    private PictureMapping pictureMapping;

    @Override
    @Transactional
    public void addProblem(Problem problem) {
        baseMapper.insert(problem);
        switch (problem.getType()) {
            case 1: {
                problem.getExpend().forEach(t -> {
                    Choice c = BeanUtil.fillBeanWithMap((Map<?, ?>) t, new Choice(), false);
                    c.setProblemId(problem.getId());
                    choiceMapping.insert(c);
                });
                break;
            }
            case 2: {
                break;
            }
            case 3: {
                problem.getExpend().forEach(t -> {
                    Picture c = BeanUtil.fillBeanWithMap((Map<?, ?>) t, new Picture(), false);
                    c.setProblemId(problem.getId());
                    pictureMapping.insert(c);
                });
                break;
            }
            default: {
                throw new RuntimeException("Problem Type Error!");
            }
        }
        problem.setId(null);

    }

    @Override
    public Page<Problem> getProblem(Search search) {
        QueryWrapper<Problem> wrapper = new QueryWrapper<Problem>();
        if (search.getKeywords() != null) {
            wrapper.like("title", "%" + search.getKeywords() + "%");
        }
        if (search.getType() != null) {
            wrapper = wrapper.eq("type", search.getType());
        }
        if (search.getKnowledgeIds() != null && search.getKnowledgeIds().size() != 0) {
            wrapper.in("knowledge_id", search.getKnowledgeIds());
        }
        if (search.getDifficulty() != null && search.getDifficulty().size() != 0) {
            wrapper.in("difficulty", search.getKnowledgeIds());
        }
        Page<Problem> result = new Page<>(search.getPage(), search.getLimit());
        baseMapper.selectPage(result, wrapper);
        return result;
    }

    public Problem getProblemById(Long id){
        Problem problem = baseMapper.selectById(id);
        if(problem.getType()==1){
            problem.setExpend(choiceMapping.selectByMap(Map.of("problem_id", id)));
        }
        return problem;
    }

    @Override
    @Transactional
    public void updateProblem(Problem problem) {
        baseMapper.updateById(problem);
        switch (problem.getType()) {
            case 1: {
                for (Object o : problem.getExpend()) {
                    Choice choice = (Choice) o;
                    if (choice.getId() == null) {
                        choiceMapping.insert(choice);
                    } else {
                        choiceMapping.updateById(choice);
                    }
                }
                break;
            }
            case 2: {
                break;
            }
            case 3: {
                for (Object o : problem.getExpend()) {
                    Picture picture = (Picture) o;
                    if (picture.getId() == null) {
                        pictureMapping.insert(picture);
                    } else {
                        pictureMapping.updateById(picture);
                    }
                }
                break;
            }
            default: {
                throw new RuntimeException("Problem Type Error!");
            }
        }
    }



}
